Creating custom animations
Creating custom animations to tween GameObjects is very easy to do. You start by creating a new class which inherits from EAnimation.
using UnityEngine;
using ExtensionTools.Animations;
public class ECustomAnimation: EAnimation
{
public ECustomAnimation()
{
m_OverrideRotation = false;
m_OverrideScale = false;
m_OverridePosition = false;
}
public override Vector3 GetPosition(float animPercentage)
{
return Vector3.zero;
}
public override Quaternion GetRotation(float animPercentage)
{
return Quaternion.identity;
}
public override Vector3 GetScale(float animPercentage)
{
return Vector3.zero;
}
}
In the constructor you decide what this animation will affect. In this example we will create an animation which moves from point A to B in a waving pattern, so we only want the position to be changed.
m_OverridePosition = true;
To be able to know where this animation starts and ends we need to pass these values through the constructor, additionally we also want to know the height of the waves.
Vector3 StartPoint;
Vector3 EndPoint;
float WaveHeight;
public ECustomAnimation(Vector3 start,Vector3 end,float waveheight)
{
StartPoint = start;
EndPoint = end;
WaveHeight = waveheight;
m_OverridePosition = true;
m_OverrideRotation = false;
m_OverrideScale = false;
}
Now we can use these values in our GetPosition method to return the animated position.
public override Vector3 GetPosition(float animPercentage)
{
float WaveScale = 10f; //This could also be a variable
Vector3 interPolatedPosition = Vector3.LerpUnclamped(StartPoint, EndPoint, animPercentage);
interPolatedPosition += Vector3.up * WaveHeight * Mathf.Sin(animPercentage * WaveScale); //We return the sine wave and add it to the interpolated position;
return interPolatedPosition;
}
Our animation is finished! Now we can play the animation like this
transform.PlayAnimation(new ECustomAnimation(transform.position, targetPosition, waveHeight),time);
Using easings
Easings allow us to change the way we interpolate in our animation. Adding easing to our previous animation is very easy to do. First we add an easingType as a parameter in our constructor.
EasingType EasingType;
public ECustomAnimation(Vector3 start,Vector3 end,float waveHeight,EasingType easingType=EasingType.SmoothOut)
{
StartPoint = start;
EndPoint = end;
WaveHeight = waveHeight;
EasingType = easingType;
m_OverridePosition = true;
m_OverrideRotation = false;
m_OverrideScale = false;
}
Now we can go back to our GetPosition method and change the animPercentage like this
public override Vector3 GetPosition(float animPercentage)
{
float WaveScale = 10f; //This could also be a variable
animPercentage = Easings.Evaluate(animPercentage, EasingType); //We apply easing
Vector3 interPolatedPosition = Vector3.LerpUnclamped(StartPoint, EndPoint, animPercentage);
interPolatedPosition += Vector3.up * WaveHeight * Mathf.Sin(animPercentage * WaveScale); //We return the sine wave and add it to the interpolated position;
return interPolatedPosition;
}